package com.hao.tool.web.module.navigation.controller;

import com.hao.tool.common.enums.ShortEnum;
import com.hao.tool.web.module.base.entity.ShortInfo;
import com.hao.tool.web.module.base.service.ShortInfoService;
import com.hao.tool.web.module.navigation.entity.NoticeInfo;
import com.hao.tool.web.module.navigation.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * description
 *
 * @author RuiHaoZ on GZ.cmhk
 * @date 2019-11-22 22:17
 */
@Controller
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;
    @Autowired
    private ShortInfoService shortInfoService;

    @GetMapping("")
    public ModelAndView index() {
        List<NoticeInfo> noticeInfoList = noticeService.getLastNotice(5);
        NoticeInfo mainNotice = noticeService.getMain();
        ModelAndView mv = new ModelAndView("notice/list");
        mv.addObject("noticeList", noticeInfoList);
        mv.addObject("main", mainNotice);

        return mv;
    }

    @RequestMapping("/add")
    public ModelAndView toAdd() {
        ModelAndView mv = new ModelAndView("notice/add");
        return mv;
    }

    @RequestMapping("/pull")
    public ModelAndView toRoll() {
        ModelAndView mv = new ModelAndView("notice/pull");
        return mv;
    }

    @RequestMapping("/edit/{notId}")
    public ModelAndView toAdd(@PathVariable String notId) {
        ModelAndView mv = new ModelAndView("notice/edit");
        if (notId == null || notId.equals("0")) {
            notId = noticeService.getLastNotice();
        }
        NoticeInfo noticeInfo = noticeService.getById(notId);

        if (noticeInfo == null) {
            mv.setViewName("common/404");
        } else {
            mv.addObject("notice", noticeInfo);
        }
        mv.addObject("notId", notId);
        mv.addObject("operateType", "edit");
        return mv;
    }

    @RequestMapping("/info/{notId}")
    public ModelAndView toList(@PathVariable String notId) {
        ModelAndView mv = new ModelAndView("notice/info");

        if (notId == null || notId.equals("0")) {
            notId = noticeService.getLastNotice();
        }
        NoticeInfo noticeInfo = noticeService.getById(notId);

        ShortInfo shortInfo = shortInfoService.find(ShortEnum.FILE, noticeInfo.getId());
        if (shortInfo != null) {
            mv.addObject("shortId", shortInfo.getId());
        } else {
            mv.addObject("shortId", "无");
        }
        mv.addObject("notice", noticeInfo);
        return mv;
    }

    @RequestMapping("/listTable")
    public ModelAndView toListTable() {
        ModelAndView mv = new ModelAndView("notice/listTable");
        return mv;
    }


    //http://www.freesion.com/article/366022811/
}
